using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Xml;
using System.Data;


namespace Pampa.WebControls
{
    #region Selector

    [ToolboxData("<{0}:Selector runat=server></{0}:Selector>")]
    [DesignerAttribute(typeof(SelectorDesigner), typeof(System.ComponentModel.Design.IDesigner))]
    public class Selector : BaseControl
    {
        protected string dataHandler;
        protected string aValue;
        protected string description;

        protected int size = 5;
        protected int maxLength = 0; 
        protected bool active = true;

        protected string onClientSelect;
        protected string onClientUnselect;

        public Selector()
        {
            this.CssClass = "selectorDefault";
            this.clientClass = "WebSelector";
            this.MinimumInputLength = 3;
            this.FreeText = false;
        }

        protected override void OnLoad(EventArgs e)
        {
            // registro el estilo y las librerias javascript en la pagina
            if (this.CssClass == "selectorDefault")
                RegisterStyleSheet("Pampa.WebControls/Selector.css");
            
            RegisterLibrary("Pampa.Common/Common.js"); 
            RegisterLibrary("Pampa.WebControls/FocusManager.js");
            RegisterLibrary("Pampa.WebControls/Selector.js");

            // recupero los items y el valor tomado en el Customer
            if (Page.IsPostBack)
                LoadValue();

            base.OnLoad(e);
        }

        #region Propiedades

        public string DataHandler
        {
            get{ return dataHandler; }
            set{ dataHandler = value; }
        }

        public string Value
        {
            get { return aValue; }
            set { aValue = value; }
        }

        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        public int Size
        {
            get { return size; }
            set { size = value; }
        }

        public int MaxLength
        {
            get { return maxLength; }
            set { maxLength = value; }
        }
        
        public bool Active
        {
            get { return active; }
            set { active = value; } 
        }

        public bool Multiple
        {
            get; set;
        }

        public string EmptyText
        {
            get; set;
        }

        public int MinimumInputLength
        {
            get; set;
        }

        public bool FreeText
        {
            get; set;
        }

        public string OnClientSelect
        {
            get { return onClientSelect; }
            set { onClientSelect = value; } 
        }

        public string OnClientUnselect
        {
            get { return onClientUnselect; }
            set { onClientUnselect = value; }
        }

        protected string ObjectName
        {
            get { return "lst" + this.ID.Replace(".", "_"); }
        }

        #endregion

        #region Render

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddAttribute("id", "cont_" + this.ID);
            writer.AddAttribute("class", this.CssClass);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.RenderEndTag();

            RenderDefinitions(writer);
        }

        /// <summary>
        /// Construyo el objeto Customer que representa al combo
        /// </summary>
        private void RenderDefinitions(HtmlTextWriter output)
        {
            string objectName = this.ObjectName;

            output.WriteLine("<script type=\"text/javascript\">");
            output.WriteLine("var " + objectName + " = new " + this.clientClass + "(\"" + this.ID + "\");");

            // Properties                
            RenderProperties(output);

            // Show
            output.WriteLine(objectName + ".show(\"cont_" + this.ID + "\");");
            output.WriteLine("</script>");
        }

        protected virtual void RenderProperties(HtmlTextWriter output)
        {
            string objectName = this.ObjectName;

            output.WriteLine(objectName + ".getListFunction = \"" + this.DataHandler + "\";");
            output.WriteLine(objectName + ".size = " + this.Size + ";");
            output.WriteLine(objectName + ".description = \"" + this.Description + "\";");
            output.WriteLine(objectName + ".value = \"" + this.Value + "\";");
            output.WriteLine(objectName + ".enabled = " + this.Enabled.ToString().ToLower() + ";");
            output.WriteLine(objectName + ".active = " + this.Active.ToString().ToLower() + ";");
            output.WriteLine(objectName + ".multiple = " + this.Multiple.ToString().ToLower() + ";");
            output.WriteLine(objectName + ".emptyText = \"" + this.EmptyText + "\";");
            output.WriteLine(objectName + ".minimumInputLength = " + this.MinimumInputLength + ";");
            output.WriteLine(objectName + ".freeText = " + this.FreeText.ToString().ToLower() + ";");
            output.WriteLine(objectName + ".width = " + this.Width.Value.ToString() + ";");
            output.WriteLine(objectName + ".maxlength = " + this.MaxLength.ToString() + ";");
            output.WriteLine(objectName + ".cssClass = \"" + this.CssClass + "\";");
            output.WriteLine(objectName + ".onSelect = \"" + this.OnClientSelect + "\";");
            output.WriteLine(objectName + ".onUnselect = \"" + this.onClientUnselect + "\";");
            output.WriteLine(objectName + ".containerID = \"cont_" + this.ID + "\";");
        }
        
        #endregion

        #region Recover Client Data

        private void LoadValue()
        {
            if (Page.Request[this.ID + "_desc"] != null)
            {
                if (this.Active)
                    this.Description = Page.Request[this.ID + "_desc"];
                else
                    this.Description = Page.Request[this.ID];
            }

            if (Page.Request[this.ID + "_value"] != null)
            {
                this.Value = Page.Request[this.ID + "_value"];
            }

            if (Page.Request[this.ID + "_enabled"] != null)
            {
                this.Enabled = bool.Parse(Page.Request[this.ID + "_enabled"]);
            }

            if (Page.Request[this.ID + "_active"] != null)
            {
                this.Active = bool.Parse(Page.Request[this.ID + "_active"]);
            }
        }

        #endregion
    }
    #endregion

    #region Designer

    public class SelectorDesigner : BaseDesigner
    {
        protected override void RenderContainer(HtmlTextWriter writer, WebControl control)
        {
            writer.AddAttribute("id", control.ID);
            writer.AddStyleAttribute("height", "20px");
            writer.AddStyleAttribute("width", control.Width.ToString());

            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            writer.WriteLine(control.ID);

            writer.RenderEndTag();
        }
    }

    #endregion
}
